package com.ldx.cas;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

public class CASDemo {
    public static void main(String[] args) {
        //cas compareAndSet:比较并交换
        AtomicInteger atomicInteger = new AtomicInteger(2020);


        //期望、更新
        //public final boolean compareAndSet(int expect, int update)
        //如果我期望的值达到了，那么就更新，否则，就不更新,CAS是CPU的并发原语
        //===========捣乱的线程==============
        System.out.println(atomicInteger.compareAndSet(2020, 2021));
        System.out.println(atomicInteger.get());

        System.out.println(atomicInteger.compareAndSet(2021, 2020));
        System.out.println(atomicInteger.get());
//        atomicInteger.getAndIncrement();

        //===========期待的线程==============
        System.out.println(atomicInteger.compareAndSet(2020, 6666));
        System.out.println(atomicInteger.get());
    }
}
